package com.qs.eggs.view.activity;

import android.app.Activity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.adapter.FootAdapter;
import com.qs.eggs.base.activity.BaseActivity;
import com.qs.eggs.bean.CollectBean;
import com.qs.eggs.refresh.widget.RecyclerRefreshLayout;

import java.lang.reflect.Type;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Response;

public class FootPrintActivity extends BaseActivity implements View.OnClickListener {
    public static final int ACTION_EDIT = 1;
    public static final int ACTION_CAMPLATE = 2;
    private RecyclerView recyclerView;
    private FootAdapter footAdapter;
    private TextView mEdit;
    protected RecyclerRefreshLayout mRefreshLayout;
    protected boolean isRefreshing;
    private ImageView fanhui;
    public static Activity instance;

    @Override
    protected int getContentView() {
        return R.layout.activity_foot_print;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        instance=this;
        recyclerView = (RecyclerView) findViewById(R.id.collect_recyclerview);
        mEdit = (TextView) findViewById(R.id.tv_edit);
        fanhui = (ImageView) findViewById(R.id.fanhui);
        mEdit.setTag(ACTION_EDIT);
        mEdit.setOnClickListener(this);
        getData();
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FootPrintActivity.this.finish();
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
    }
    //获得收藏列表
    public void getData(){
        OkGo.post(Constants.URL_BASE + "lookhistory?action=searchLookHistory")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("我的足迹==" + s);
                        Type type = new TypeToken<ArrayList<CollectBean>>() {
                        }.getType();

                        GsonBuilder gsonBuilder = new GsonBuilder();
                        ArrayList<CollectBean> resultBean = gsonBuilder.create().fromJson(s, type);
                        footAdapter = new FootAdapter(FootPrintActivity.this, resultBean, mEdit);
                        recyclerView.setAdapter(footAdapter);
                        recyclerView.setLayoutManager(new LinearLayoutManager(FootPrintActivity.this));
                        recyclerView.setItemAnimator(new DefaultItemAnimator());

                    }
                });
}
    @Override
    public void onClick(View v) {
        int action = (int) v.getTag();
        if (ACTION_EDIT == action) {
            for (int i = 0; i < recyclerView.getChildCount(); i++) {
                View view = recyclerView.getChildAt(i);
                ImageView img = (ImageView) view.findViewById(
                        R.id.img_edit);
                img.setImageResource(R.mipmap.icon_del);
            }
            mEdit.setText("完成");
            mEdit.setTag(ACTION_CAMPLATE);
           footAdapter.setEdit(ACTION_EDIT);

        } else if (ACTION_CAMPLATE == action){
            for (int i = 0; i < recyclerView.getChildCount(); i++) {
                View view = recyclerView.getChildAt(i);
                ImageView img = (ImageView) view.findViewById(
                        R.id.img_edit);
                img.setImageResource(R.mipmap.icon_cart);
            }
        mEdit.setText("编辑");
        mEdit.setTag(ACTION_EDIT);
        footAdapter.setEdit(ACTION_CAMPLATE);
        getData();

        }
    }

}
